package cn.darkhorse.mesadmin.controller.system;


import cn.darkhorse.device.pojo.MesRepairInfo;
import cn.darkhorse.device.service.MesRepairInfoService;
import cn.darkhorse.messystem.pojo.MesSystemOptions;
import cn.darkhorse.messystem.service.MesSystemOptionsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统配置表（报修审核开关） 前端控制器
 * </p>
 *
 * @author yangtianqi
 * @since 2024-05-09
 */
@RestController
@RequestMapping("/messystem/mesSystemOptions")
public class MesSystemOptionsController extends BaseController {
    @Autowired
    private MesSystemOptionsService mesSystemOptionsService;

    @Autowired
    private MesRepairInfoService repairInfoService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查看
     * @param
     * @return
     */
    @RequestMapping("/view")
    public AjaxResult view(@DefaultValue(value = "system") String key){
        MesSystemOptions options=mesSystemOptionsService.view(key);
        return AjaxResult.success(options);
    }

    /**
     * 保存
     * @param options
     * @return
     */
    @RequestMapping("/save")
    public AjaxResult save(@RequestBody MesSystemOptions options){
        boolean result=mesSystemOptionsService.saveOptions(options);
        return AjaxResult.success(result);
    }



    /**
     * 校验是否开启审核以及是否有审核权限
     */
    @RequestMapping("/authentication-audit")
    public AjaxResult authenticationAudit(@DefaultValue(value = "system") String key){

        return AjaxResult.success(mesSystemOptionsService.authenticationAudit(key));
    }

    /**
     * 校验是否开启审核以及是否有验证权限
     */
    @RequestMapping("/authentication-verify")
    public AjaxResult authenticationVerify(@DefaultValue(value = "system") String key, Long repairInfoId){

        MesRepairInfo repairInfo = repairInfoService.getById(repairInfoId);
        return AjaxResult.success(mesSystemOptionsService.authenticationVerify(key,repairInfo.getCreator()));
    }

    /**
     * 根据配置的审核角色组获取下一审核人可选列表
     * @param key
     * @return
     */
    @RequestMapping("/list-hasRole")
    public AjaxResult hasRoleUsersList(@DefaultValue(value = "system") String key){
        MesSystemOptions options=mesSystemOptionsService.view(key);
        if (options.getEnableRepairAudit().equals(0)){
            return AjaxResult.success("审核未开启");
        }
        if (options.getRepairAuditRoles().isEmpty()){
            return AjaxResult.success();
        }
        List<SysUser> users = userService.selectHasRoleList(options.getRepairAuditRoles());
        return AjaxResult.success(users);
    }
}

